package com.example.backend.controller.brand;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.dto.request.BrandsDto;
import com.example.backend.dto.request.PageUserDto;
import com.example.backend.entitly.mysql.order.Brands;
import com.example.backend.service.mysql.order.BrandsService;
import com.example.backend.service.storage.MinIOService;
import com.example.backend.utils.ResultJson;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/brand")
@Tag(name = "品牌管理")
public class BrandController {
    private final BrandsService brandsService;
    private final MinIOService minIOService;

    @Transactional
    @Operation(summary = "创建品牌", description = "创建品牌")
    @PostMapping("/create")
    public ResultJson<?> createBrand(
            @RequestPart("brandName") String brandName,
            @RequestPart("description") String description,
            @RequestPart("logoUrl") MultipartFile logoUrl) throws Exception {
        log.debug("创建品牌名称: {}， 品牌描述：{}， ", brandName, description);
        String url = minIOService.uploadAvatar(logoUrl, brandName);
        Brands brands = new Brands();
        brands.setBrandName(brandName);
        brands.setDescription(description);
        brands.setLogoUrl(url);
        brandsService.save(brands);
        return ResultJson.ok();
    }

    @Transactional
    @Operation(summary = "更新品牌", description = "更新品牌")
    @PostMapping("/update")
    public ResultJson<?> updateBrand(
            @RequestBody BrandsDto brandsDto
    ) {
        log.debug("更新品牌名称: {}， 品牌描述：{}， ", brandsDto.getBrandName(), brandsDto.getDescription());
        Brands convert = Convert.convert(Brands.class, brandsDto);
        boolean b = brandsService.saveOrUpdate(convert);
        if (!b) {
            return ResultJson.fail("更新失败");
        }
        return ResultJson.ok();
    }

    @Transactional
    @Operation(summary = "更新头像", description = "更新头像")
    @PostMapping("/updateAvatar")
    public ResultJson<?> updateAvatar(
            @RequestPart("brandId") Integer brandId,
            @RequestPart("logoUrl") MultipartFile logoUrl
    ) throws Exception {
        log.debug("更新品牌名称: {}， ", brandId);
        Brands byId = brandsService.getById(brandId);
        if (byId == null) {
            return ResultJson.fail("品牌不存在");
        }
        String url = minIOService.uploadAvatar(logoUrl, brandId.toString());
        minIOService.deleteAvatar(byId.getLogoUrl());
        Brands brands = new Brands();
        brands.setBrandId(brandId);
        brands.setLogoUrl(url);
        boolean b = brandsService.updateById(brands);
        if (!b) {
            return ResultJson.fail("更新失败");
        }
        return ResultJson.ok();
    }

    @Transactional
    @Operation(summary = "删除品牌", description = "删除品牌")
    @PostMapping("/delete")
    public ResultJson<?> deleteBrand(
            @RequestParam("brandId") Integer brandId
    ) {
        log.debug("删除品牌名称: {}， ", brandId);
        boolean b = brandsService.removeById(brandId);
        if (!b) {
            return ResultJson.fail("删除失败");
        }
        return ResultJson.ok();
    }

    @Transactional
    @Operation(summary = "查询品牌", description = "查询品牌")
    @GetMapping("/query")
    public ResultJson<Brands> queryBrand(
            @RequestParam("brandId") Integer brandId
    ) {
        log.debug("查询品牌名称: {}， ", brandId);
        Brands byId = brandsService.getById(brandId);
        return ResultJson.success(byId);
    }

    @Transactional
    @Operation(summary = "查询所有品牌", description = "查询所有品牌")
    @GetMapping("/queryAll")
    public ResultJson<List<Brands>> queryAllBrand(@RequestBody PageUserDto pageUserDto) {
        log.debug("查询所有品牌");
        Page<Brands> brandsPage = brandsService.pageGetAll(pageUserDto);
        return ResultJson.success(brandsPage.getRecords());
    }
}
